package cn.john.user.service.impl;

import cn.john.basic.exception.BusinessException;
import cn.john.basic.service.impl.BaseServiceImpl;
import cn.john.basic.util.MD5Utils;
import cn.john.basic.util.StrUtils;
import cn.john.user.constants.UserConstants;
import cn.john.user.domain.LoginInfo;
import cn.john.user.domain.User;
import cn.john.user.dto.UserDto;
import cn.john.user.mapper.LoginInfoMapper;
import cn.john.user.mapper.UserMapper;
import cn.john.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void phoneRegister(UserDto userDto) {
        // 获取用户电话
        String phone = userDto.getPhone();
        // 1. 基本校验
        // 1.1 空校验
        if (StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(userDto.getVerifyCode())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessException("请输入完整信息再注册!");
        }
        // 1.2 校验密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessException("两次密码不一致!请重新填写密码!");
        }
        // 1.3 校验手机号是否已经被注册
        User userTmp = userMapper.loadByPhone(phone);
        if (userTmp != null) {
            throw new BusinessException("该手机号已被注册!");
        }
        // 2. 验证码校验
        Object objTmp = redisTemplate.opsForValue().get(UserConstants.REGISTER_PHONE_PREFIX + phone);
        // 2.1 判断从redis中能否拿到验证码，如果没有拿到验证码，报错
        if (objTmp == null) {
            throw new BusinessException("请获取并填写验证码!");
        }
        // 2.2 拿到了验证码，判断验证码是否一致,如果不一致，报错
        String code = ((String) objTmp).split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码错误!");
        }
        // 3.注册
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);
        // 3.1 保存logininfo 并返回自增id
        loginInfoMapper.save(loginInfo);
        // 3.2 保存user
        user.setLoginInfo(loginInfo);
        userMapper.save(user);
    }

    private User userDto2User(UserDto userDto) {
        // 1. 定义返回值对象
        User user = new User();
        // 2. 给对象赋值
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 设置盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        // 设置盐值和密码
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        // 3. 返回对象
        return user;
    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        // 拷贝，同名原则
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }
}
